class Solution(object):
    def maxIncreaseKeepingSkyline(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        m = len(grid)
        n = len(grid[0])
        max_h = [0] * m
        max_v = [0] * n
        for i in range(m):
            max_h[i] = max(grid[i])
        for j in range(n):
            max_v[j] = max([grid[i][j] for i in range(m)])
        t = 0
        for i in range(m):
            for j in range(n):
                t += min(max_h[i] - grid[i][j], max_v[j] - grid[i][j])
        return t



data = Solution()
grid = [[3, 0, 8, 4], [2, 4, 5, 7], [9, 2, 6, 3], [0, 3, 1, 0]]
print(data.maxIncreaseKeepingSkyline(grid))
